package com.china_goldcard.myactionsheet;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

/**
 * adapter
 */
public class MyActionSheetAdapter extends RecyclerView.Adapter<MyActionSheetAdapter.MyActionSheetViewHolder> {

    private List<String> dataList;

    private Context context;

    private int resource;

    private float itemH;

    private LayoutInflater layoutInflater;

    public interface OnItemClickListener{
        void onItemClicked(String item,int position);
    }

    public OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setItemH(float itemH) {
        this.itemH = itemH;
    }

    public MyActionSheetAdapter(Context context, List<String> dataList, int resource){
        this.context = context;
        this.dataList = dataList;
        this.resource = resource;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public MyActionSheetViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(resource,parent,false);
        view.getLayoutParams().height = (int)itemH;
        return new MyActionSheetViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyActionSheetViewHolder holder, final int position) {
        final String item = dataList.get(position);
        holder.textView.setText(item);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener !=null){
                    onItemClickListener.onItemClicked(item,position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public class MyActionSheetViewHolder extends  RecyclerView.ViewHolder{

        TextView textView;

        public MyActionSheetViewHolder(View itemView) {
            super(itemView);
            textView = (TextView)itemView.findViewById(R.id.my_action_sheet_item_tv);
        }
    }
}
